package 力扣日常刷题.木22二月.第06天212;

/**
 * @author 帅小伙
 * @date 2022/2/12
 * @description
 */
public class Demo47贪心加油站 {


    // sum 表示从0 - i所剩油量  可能是负
    // minSurplus  0 - i最小剩余
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int index = -1,minSurplus = Integer.MAX_VALUE;
        int sum = 0;
        for (int i = 0; i < gas.length; i++) {
            sum = sum + gas[i] - cost[i];
            if(sum < minSurplus){
                minSurplus = sum;
                index = i;
            }
        }
        return sum >= 0 ?  (index+1) % gas.length : -1;
    }

}
